package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "商品分类接口管理")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {
    @Autowired
    ICategoryService categoryService;

    @GetMapping(value = "/getOneCategory")
    public R<List<CategoryVo>> getOneCategory() {
        return R.ok(categoryService.getOneCategory());
    }

    @Operation(summary = "获取分类下拉树列表")
    @GetMapping("/treeSelect/{parentId}")
    public AjaxResult treeSelect(@PathVariable Long parentId){
        return AjaxResult.success( categoryService.treeSelect(parentId));
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response){
        categoryService.exportData(response);
    }
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file)throws Exception{
        try {
            categoryService.importData(file);
            return AjaxResult.success("导入成功");
        }catch (Exception e){
            e.printStackTrace();
        }

        return AjaxResult.error("导入失败");
    }

    @InnerAuth
    @GetMapping(value = "/tree")
    public R<List<CategoryVo> > tree() {
        return R.ok(categoryService.tree());
    }


}
